﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Microsoft.Xna.Framework;

namespace ChuyenDongVatLy
{
    public struct Line
    {
        //ax+by=c
        private float a;
        private float b;
        private float c;
        public float A
        {
            get { return a; }
            set { a = value; }
        }        
        public float B
        {
            get { return b; }
            set { b = value; }
        }       
        public float C
        {
            get { return c; }
            set { c = value; }
        }
       public Line(float a1, float b1, float c1)
        {
            a = a1;
            b = b1;
            c = c1;
        }
    }

    public class Geometry
    {   
        public Geometry(){}
        public Line DuongThangQua2Diem(Vector2 A,Vector2 B)
        {
            float a, b, c;
            a = A.Y - B.Y;
            b = -1 * (A.X - B.X);
            c = A.X * (A.Y - B.Y) - A.Y * (A.X - B.X);
            return new Line(a, b, c);
        }
        public Line DTQua1DVuongGoc1DT(Vector2 A, Line line)
        {
            float a, b, c;
            a = -1 * line.B;
            b = line.A;
            c = a * A.X + b * A.Y;
            return new Line(a, b, c);
        }
        public Vector2 DiemGiaoNhau(Line l1, Line l2)
        {

            float D, Dx, Dy;
            D = l1.A * l2.B - l1.B * l2.A;
            Dx = l1.C * l2.B - l1.B * l2.C;
            Dy= l1.A*l2.C-l1.C*l2.A;
            float x, y;
            x= Dx/D;
            y= Dy/D;
            return new Vector2(x, y);
        }
        public float DistancePointAndLine(Vector2 diem, Line line)
        {
            Line l1 = DTQua1DVuongGoc1DT(diem, line);
            Vector2 d1 = DiemGiaoNhau(l1, line);
            float distance =(float)Math.Sqrt(Math.Pow(diem.X - d1.X, 2) + Math.Pow(diem.Y - d1.Y, 2));
            return distance;
        }
    }
}
